import datetime
from django.db import models
from django.utils import timezone

# Create your models here.

class Question(models.Model):
    question_text = models.CharField(max_length=200)    # 字符-200长度
    pub_date = models.DateTimeField("date published")   # 时间格式 方便人去读

    # 判断是不是近期发布的
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

    def __str__(self):
        return self.question_text


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)    # 外键
    """
    Question,             # 关联的模型类（自身或其他模型）
    on_delete=models.CASCADE  # 删除关联对象时的行为,当关联的Question对象被删除时，级联删除当前对象（即 “父删子也删”）
    """
    choice_text = models.CharField(max_length=200)  # 字符-200长度
    votes = models.IntegerField(default=0)          # 整数，默认赋值0

    def __str__(self):
        return self.choice_text

class Book(models.Model):
    name = models.CharField(max_length=200)
    cbans = models.CharField(max_length=200)

    def __str__(self):
        return self.name

class NewsUser(models.Model):
    username = models.CharField(max_length=200)
    password = models.CharField(max_length=200)
    email = models.CharField(max_length=200)
    GENDER_CHOICES = [
        (1, '男'),
        (2, '女')
    ]
    gender = models.IntegerField(choices=GENDER_CHOICES, default=1)

    def __str__(self):
        return self.username